package com.liug.droidsmall2.presenter;

import android.util.Log;

import com.liug.droidsmall2.entities.Environment;
import com.liug.droidsmall2.entities.HomeData;
import com.liug.droidsmall2.entities.XMLService;
import com.liug.droidsmall2.fragment.RecruitFragment;
import com.liug.droidsmall2.interfaces.GankApiService;
import com.liug.droidsmall2.interfaces.IUpdateApi;
import com.liug.droidsmall2.utils.net.RetrofitUtils;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import rx.Observable;
import rx.Subscriber;
import rx.functions.Func1;
import rx.schedulers.Schedulers;

/**
 * Created by LG on 2018/7/31.
 */

public class RecruitPresenter {
    private HomeData.DataBean data;
    private RecruitFragment frag;

    public RecruitPresenter(RecruitFragment frag) {
        this.frag = frag;
    }

    /**
     * 获取招聘数据
     */
    public void getHomeData() {
        Retrofit init = RetrofitUtils.init();
        GankApiService gankApiService = init.create(GankApiService.class);
        Call<HomeData> homeData = gankApiService.getHomeData("1025");
        homeData.enqueue(new Callback<HomeData>() {
            @Override
            public void onResponse(Call<HomeData> call, Response<HomeData> response) {
                HomeData body = response.body();
                if (body.getCode() == 200) {
                    data = body.getData();
                    frag.setData(data);
                }
            }
            @Override
            public void onFailure(Call<HomeData> call, Throwable t) {
                frag.setData(null);
            }
        });
    }

    /**
     * 获取天气数据
     */
    public void getWeatherData() {
//http://wthrcdn.etouch.cn/WeatherApi?citykey=101010100
        Retrofit init = RetrofitUtils.initWeather();
        IUpdateApi iUpdateApi = init.create(IUpdateApi.class);
        Observable<XMLService> weather = iUpdateApi.getWeather("101010100");
        weather.subscribeOn(Schedulers.io());
//        weather.observeOn(AndroidScheduler.mainThread());
        weather.flatMap(new Func1<XMLService, Observable<Environment>>() {
            @Override
            public Observable<Environment> call(XMLService xmlService) {
                return Observable.from(xmlService.newsXmls);
            }
        });

        weather.subscribe(new Subscriber<XMLService>() {
            @Override
            public void onCompleted() {
                Log.v("xmlidea", "onCompleted");
            }

            @Override
            public void onError(Throwable e) {
                Log.v("xmlidea", "onError" + e.toString());
            }

            @Override
            public void onNext(XMLService xmlService) {
                Log.v("xmlidea", xmlService.city);
                Log.v("xmlidea", xmlService.fengxiang);
            }
        });

    }
}
